package com.sddnytsh.body_buildingsystem.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sddnytsh.body_buildingsystem.topic.RunData
import java.time.format.DateTimeFormatter

@Composable
fun WorkoutDataTable(data: List<RunData>) {
    Card(
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(12.dp)
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Text("详细运动数据",
                style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold))

            Spacer(modifier = Modifier.height(16.dp))

            // 表头
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(Color(0xFFF5F5F5))
                    .padding(vertical = 12.dp, horizontal = 8.dp)
            ) {
                Text(
                    text = "日期",
                    fontWeight = FontWeight.Bold,
                    fontSize = 13.sp,
                    color = Color(0xFF757575),
                    modifier = Modifier.weight(0.25f),
                    textAlign = TextAlign.Center
                )
                Text(
                    text = "卡路里",
                    fontWeight = FontWeight.Bold,
                    fontSize = 13.sp,
                    color = Color(0xFF757575),
                    modifier = Modifier.weight(0.2f),
                    textAlign = TextAlign.Center
                )
                Text(
                    text = "步数",
                    fontWeight = FontWeight.Bold,
                    fontSize = 13.sp,
                    color = Color(0xFF757575),
                    modifier = Modifier.weight(0.2f),
                    textAlign = TextAlign.Center
                )
                Text(
                    text = "时长",
                    fontWeight = FontWeight.Bold,
                    fontSize = 13.sp,
                    color = Color(0xFF757575),
                    modifier = Modifier.weight(0.2f),
                    textAlign = TextAlign.Center
                )
                Text(
                    text = "距离",
                    fontWeight = FontWeight.Bold,
                    fontSize = 13.sp,
                    color = Color(0xFF757575),
                    modifier = Modifier.weight(0.15f),
                    textAlign = TextAlign.Center
                )
            }

            // 表格数据行
            data.forEachIndexed { index, dayData ->
                TableRow(dayData, index % 2 == 0)
                if (index < data.size - 1) {
                    Divider(
                        color = Color(0xFFEEEEEE),
                        thickness = 1.dp,
                        modifier = Modifier.padding(horizontal = 8.dp)
                    )
                }
            }
        }
    }
}

@Composable
fun TableRow(data: RunData, isEven: Boolean) {
    val backgroundColor = if (isEven) Color.White else Color(0xFFFAFAFA)

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(backgroundColor)
            .padding(vertical = 12.dp, horizontal = 8.dp),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(
            text = data.date.format(DateTimeFormatter.ofPattern("MM/dd")),
            fontSize = 12.sp,
            modifier = Modifier.weight(0.25f),
            textAlign = TextAlign.Center
        )
        Text(
            text = "${data.calories}",
            fontSize = 12.sp,
            fontWeight = FontWeight.Medium,
            color = Color(0xFFF44336),
            modifier = Modifier.weight(0.2f),
            textAlign = TextAlign.Center
        )
        Text(
            text = "${data.steps}",
            fontSize = 12.sp,
            modifier = Modifier.weight(0.2f),
            textAlign = TextAlign.Center
        )
        Text(
            text = "${data.duration}m",
            fontSize = 12.sp,
            modifier = Modifier.weight(0.2f),
            textAlign = TextAlign.Center
        )
        Text(
            text = "${String.format("%.1f", data.distance)}km",
            fontSize = 12.sp,
            modifier = Modifier.weight(0.15f),
            textAlign = TextAlign.Center
        )
    }
}